package com.longway.core.toolbox.apidoc;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Inject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.longway.common.controller.BaseController;
import com.longway.common.model.BaseSetting;
import com.longway.common.service.BusinessService;
import com.longway.core.toolbox.apidoc.models.ApiDocModel;
import com.longway.core.toolbox.apidoc.models.ApiMethodModel;
import com.longway.core.toolbox.apidoc.models.ApiModule;
import com.longway.core.toolbox.apidoc.models.ApiParamsModel;

/**
 * @author ryc
 * 2020年4月30日 下午2:43:35
 */
public class ApiDocController extends BaseController {

	public void index() {
		ApiDocKit.refreshApiModule();
		Map<String,List<ApiDocModel>> apiModels = ApiDocKit.getApiModule().getApiModels();
		BaseSetting setting = BusinessService.me.findBaseSettingByCache();
		setAttr("data", JsonKit.toJson(apiModels));
		if(setting != null){
			setAttr("apiTitle",setting.getApiTitle());
		}
		render("index.html");
	}
	
	public void test() {
		String controllerkey;
		String actionkey;
		try {
			controllerkey = java.net.URLDecoder.decode(get(0),"UTF-8");
			actionkey = java.net.URLDecoder.decode(get(1),"UTF-8");
			ApiModule module = ApiDocKit.getApiModule();
			ApiMethodModel action = module.getActionModel(controllerkey, actionkey);
			setAttr("action", JsonKit.toJson(action));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		render("apiDoc.html");
	}
	
	public void token() {
		render("token.html");
	}
}